package com.dingwei.superiordiary.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.LogisticsLogAdapter;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.LogisticsLogBean;
import com.dingwei.superiordiary.mvp.presenter.LogisticsLogPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.weight.TitleBar;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/3/1.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class LogisticsLogActivity extends BaseActivity implements Contract.LogisticsLogView {

    LogisticsLogPresenter logisticsLogPresenter = new LogisticsLogPresenter(this);
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.img_goods)
    ImageView imgGoods;
    @Bind(R.id.tv_staus)
    TextView tvStaus;
    @Bind(R.id.tv_logistics_num)
    TextView tvLogisticsNum;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.tv_num)
    TextView tvNum;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.data_layout)
    LinearLayout dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;


    LogisticsLogAdapter adapter;
    private String id;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_look_logistics;
    }

    @Override
    protected void initData() {
        logisticsLogPresenter.initData(id);
    }

    @Override
    protected void initView() {

        id = getIntent().getStringExtra("id");

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.line), 0));

        adapter = new LogisticsLogAdapter(this);
        mRecyclerView.setAdapter(adapter);


    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void setData(LogisticsLogBean bean) {
        Glide.with(BaseActivity.context)
                .load(bean.data.product_icon)
                .apply(MyApplication.options)
                .into(imgGoods);
        tvStaus.setText(bean.data.status);
        tvLogisticsNum.setText(bean.data.shipper + ":" + bean.data.shipper_code);
        tvPhone.setText(bean.data.tel);
        tvNum.setText("X"+bean.data.quantity);

        adapter.addAll(bean.data.log);

        showData("success");
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    public void showData(String state) {
        if (state.equals("success")) {
            dataLayout.setVisibility(View.VISIBLE);
            noNetLayout.setVisibility(View.GONE);
            if (adapter.getAllData()!=null&&adapter.getAllData().size()>0){
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            }else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }
}
